/**
 * 统计表格
 */
import dayjs from 'dayjs';

export default {
  methods: {
    async handleSetLineChartData (type, datePicker) {
      this.setline = type;
      const params = {
        startTime: dayjs(
          (datePicker && datePicker[0]) || (this.$refs.todo.datePicker && this.$refs.todo.datePicker[0])
        ).format('YYYY-MM-DD 00:00:00'),
        endTime: dayjs(
          (datePicker && datePicker[1]) || (this.$refs.todo.datePicker && this.$refs.todo.datePicker[1])
        ).format('YYYY-MM-DD 23:59:59')
      };
      if (type !== 'all') {
        params.type = type;
      }
      const { code, data, message } = await this.$API[this.api](params);

      if (code === 200) {
        this.chartData.time = [];
        this.chartData.total = [];
        let options = {};

        data && data.length > 0 && data.forEach((item, index) => {
          this.chartData.time.push(item.dayTime);
          this.chartData.total.push(item.countNum / (this.rate || 1));
          console.log(index);
          options = {
            xAxis: {
              data: this.chartData.time,
              boundaryGap: false,
              axisTick: {
                show: false
              },
              axisLabel: {
                interval: 2,
                margin: 10,
                rotate: this.chartData.time.length > 10 ? 45 : 0,
              },
            },
            grid: {
              left: 20,
              right: 20,
              bottom: 20,
              top: 30,
              containLabel: true
            },
            tooltip: {
              trigger: 'axis',
              axisPointer: {
                type: 'cross'
              },
              padding: [5, 10]
            },
            yAxis: {
              axisTick: {
                show: false
              }
            },

            series: [
              {
                name: '总量',
                itemStyle: {
                  normal: {
                    color: '#3888fa',
                    lineStyle: {
                      color: '#3888fa',
                      width: 2
                    }
                  }
                },
                smooth: true,
                type: 'line',
                data: this.chartData.total,
                animationEasing: 'cubicInOut',
                animationDuration: 2800
              }
            ]
          };
        });
        this.$BUS.$emit('line.chart', options);
      } else {
        this.$message.error(message);
      }
    }
  }
};
